/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package solucionessgh.TipoHabitacion;

/**
 *
 * @author roderick
 */


import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import solucionessgh.Util.DataBase;

public class TipoHabitacionModel extends DataBase{

    private int  id;//thacod
    private String thadesc;
    private String thaabr;
    private int thaccm;

    public TipoHabitacionModel(){

    }

    private boolean validar(){
        return true;
    }

    public boolean create(){
        if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("INSERT INTO tipohabitacion(thadesc, thaabr, thaccm)VALUES (?, ?, ?);");
                stmt.setString(1,this.thadesc);
                stmt.setString(2,this.thaabr);
                stmt.setInt(3,this.thaccm);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Tipo Habitacion",
                    JOptionPane.ERROR_MESSAGE);
            }
        }
        return false;
    }

    public boolean update(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("UPDATE tipohabitacion SET thadesc=?, thaabr=?, thaccm=? where thacod = ? ;");
                stmt.setString(1,this.thadesc);
                stmt.setString(2,this.thaabr);
                stmt.setInt(3, this.thaccm);
                stmt.setInt(4, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Tipo Habitacion",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public boolean delete(){
        //if(validar()){
            try{
                Connection con = ConnectionBase();
                PreparedStatement stmt = con.prepareStatement("delete from tipohabitacion where thacod=?;");
                stmt.setInt(1, this.id);
                stmt.executeUpdate();
                return true;
            }
            catch( Exception e ){
                JOptionPane.showMessageDialog(null,
                    e.getMessage(),
                    "Nivel Usuario",
                    JOptionPane.ERROR_MESSAGE);
            }
        //}
        return false;
    }

    public ResultSet read(){
        return this.find("SELECT * FROM tipohabitacion order by thadesc ");
    }

    public ResultSet finder(String var){
        return this.find("SELECT thacod, thadesc, thaabr, thaccm FROM tipohabitacion WHERE thadesc LIKE '"+var+"%';");
    }

    public ResultSet finder(int var){
        return this.find("SELECT thacod, thadesc, thaabr, thaccm FROM tipohabitacion WHERE thacod = "+var+";");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getThaabr() {
        return thaabr;
    }

    public void setThaabr(String thaabr) {
        this.thaabr = thaabr;
    }

    public int getThaccm() {
        return thaccm;
    }

    public void setThaccm(int thaccm) {
        this.thaccm = thaccm;
    }

    public String getThadesc() {
        return thadesc;
    }

    public void setThadesc(String thadesc) {
        this.thadesc = thadesc;
    }

}
